<?php

require_once("BaseDAO.php");
require_once("../Entities/Paragraph.php");

class ParagraphDAO extends BaseDAO
{
	function CreateParagraph($item)
	{
		$sql = "insert into Paragraphs(Content, Sound) values ('" . 
			$item->Content . "','" . 
			$item->Sound . "');";
		mysql_query($sql);
		$item->Id = mysql_insert_id();
		return $item;
	}
	
	function UpdateParagraph($para)
	{
		$sql = "update Paragraphs set Content='" . $para->Content . "',Sound='" . $para->Sound . "' where Id='" . $para->Id . "';";
		mysql_query($sql);
	}
	
	function DeleteParagraph($id)
	{
		$sql = "delete from Paragraphs where Id='" . $id . "';";
		mysql_query($sql);
	}
	
	function GetParagraphById($id)
	{
		$sql = "select * from Paragraphs where Id='" . $id . "';";
		$result = mysql_query($sql);
		if(mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_assoc($result);
			$item = new Paragraph();
			$item->Id = $row["Id"];
			$item->Content = $row["Content"];
			$item->Sound = $row["Sound"];
			return $item;
		}
		return null;
	}
	
	function GetParagraphs($page, $pageSize, &$totalPage)
	{
		$sqlTotalPage = "select * from Paragraphs";
		$sql = "select * from Paragraphs limit " . ($page - 1) * $pageSize . "," . $pageSize . ";";
		$resultTotalPage = mysql_query($sqlTotalPage);
		$result = mysql_query($sql);
		$totalPage = ceil(mysql_num_rows($resultTotalPage) / $pageSize);
		$list = array();
		while($row = mysql_fetch_assoc($result))
		{
			$item = new Paragraph();
			$item->Id = $row["Id"];
			$item->Content = $row["Content"];
			$item->Sound = $row["Sound"];
			$list[] = $item;
		}
		return $list;
	}
}

?>